//
//  ZMZSRootViewController.m
//  ZhangMiZhuShou
//
//  Created by ryou on 2021/7/26.
//

#import "ZMZSRootViewController.h"
#import "ZMZSXiangQingViewController.h"
#import "ZMZSGuanyuViewController.h"
#import "ZMZSXuanZeLeiXingViewController.h"
#import "ZMZSFenLeiViewController.h"
#import <StoreKit/StoreKit.h>

@interface ZMZSRootViewController ()
@property(nonatomic,assign)NSInteger zmxzsSelectIndex;
@end

@implementation ZMZSRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zmxzsSelectIndex=0;
    self.title = @"首页";
    
    [self performSelector:@selector(zmxzspingfen) withObject:nil afterDelay:3.0];
    
}
-(void)zmxzspingfen{
    
    NSUserDefaults *ZMZSAcountMuser=[NSUserDefaults standardUserDefaults];
    BOOL isP=[ZMZSAcountMuser boolForKey:@"zmxzspingfen"];
    if (!isP) {
        [ZMZSAcountMuser setBool:YES forKey:@"zmxzspingfen"];
        if (@available(iOS 10.3, *)) {
            [SKStoreReviewController requestReview];
        } else {
            // Fallback on earlier versions
        }
    }
    
}
-(void)viewDidAppear:(BOOL)animated{
    for (UIView *view in self.view.subviews) {
        [view removeFromSuperview];
    }
    [self.ZMXZSData removeAllObjects];
    NSUserDefaults *ZMZSAcountMuser=[NSUserDefaults standardUserDefaults];
    NSArray *data=[ZMZSAcountMuser valueForKey:@"ZMZSaccountmanagermuserdata"];
    [self.ZMXZSData addObjectsFromArray:data];
    
    if (data.count==0) {
        [self zmxzsshowzmxzsAcountMnotData];
    }else{
        [self zmxzscreateView];
    }
    [self zmxzscreateBtnView];
}
-(void)zmxzsshowzmxzsAcountMnotData{
    UIImageView *zmxzsAcountMnotData=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pm_icon_zmxzsAcountMnotData.png"]];
    zmxzsAcountMnotData.center = self.view.center;
    [self.view addSubview:zmxzsAcountMnotData];
    
    UIButton *zmxzsAcountMsavebtn=[[UIButton alloc]initWithFrame:CGRectMake(kZMXZSWidth / 2 - 70, zmxzsAcountMnotData.frame.size.height+ zmxzsAcountMnotData.frame.origin.y + 30, 140, 60)];
    [zmxzsAcountMsavebtn setBackgroundImage:[UIImage imageNamed:@"zmxzs_btn_bg.png"] forState:UIControlStateNormal];
    [self.view addSubview:zmxzsAcountMsavebtn];
    [zmxzsAcountMsavebtn setTitle:@"添加" forState:UIControlStateNormal];

    zmxzsAcountMsavebtn.layer.cornerRadius = 30;
    zmxzsAcountMsavebtn.clipsToBounds=YES;
    [zmxzsAcountMsavebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [zmxzsAcountMsavebtn addTarget:self action:@selector(zmxzspmzmxzscenterBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)zmxzscreateView{
    UIImageView *bg=[[UIImageView alloc]initWithFrame:self.view.bounds];
    bg.image=[UIImage imageNamed:@"SH-2@3x.png"];
    bg.contentMode=UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    
    CGFloat ZMZSitemW = 80;
    CGFloat ZMZSspace = (kZMXZSWidth - 80 * 3) / 4;
    
    UIWindow * window = [[UIApplication sharedApplication].delegate window];
    CGFloat bottom = 0.0;
    if (@available(iOS 11.0, *)) {
        bottom = window.safeAreaInsets.top;
    } else {
        // Fallback on earlier versions
    }
    CGFloat yyy=self.navigationController.navigationBar.frame.origin.y +  self.navigationController.navigationBar.frame.size.height + bottom;
    
    for (int i=0; i<self.ZMXZSData.count; i++) {
        NSDictionary *dict = self.ZMXZSData[i];
        UIImageView *ZMZSZMZSAcountMbgimage=[[UIImageView alloc]initWithFrame:CGRectMake(ZMZSspace +(ZMZSitemW  + ZMZSspace) * (i%3),yyy + ZMZSspace +(ZMZSitemW  + ZMZSspace) * (i / 3), ZMZSitemW, ZMZSitemW)];
        ZMZSZMZSAcountMbgimage.image=[UIImage imageNamed:@"zmxzs_icon_bgfasdfad.png"];
        ZMZSZMZSAcountMbgimage.contentMode=UIViewContentModeScaleToFill;
        ZMZSZMZSAcountMbgimage.layer.cornerRadius=10;
        
        ZMZSZMZSAcountMbgimage.clipsToBounds=YES;
        [self.view addSubview:ZMZSZMZSAcountMbgimage];
        ZMZSZMZSAcountMbgimage.tag=i;
        ZMZSZMZSAcountMbgimage.userInteractionEnabled=YES;
        UITapGestureRecognizer *zmxzspmtap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgClick:)];
        [ZMZSZMZSAcountMbgimage addGestureRecognizer:zmxzspmtap];
        
        
        UIImageView *icon=[[UIImageView alloc]initWithFrame:CGRectMake(20, 20, ZMZSZMZSAcountMbgimage.bounds.size.width - 40, ZMZSZMZSAcountMbgimage.bounds.size.height-40)];
        icon.image=[UIImage imageWithData:dict[@"ZMZSZMZSaccountmanagermuserpmbiaojipmicon"]];
        icon.contentMode=UIViewContentModeScaleAspectFill;
        icon.layer.cornerRadius=10;
        icon.clipsToBounds=YES;
        [ZMZSZMZSAcountMbgimage addSubview:icon];
        
    }
}
-(void)bgClick:(UITapGestureRecognizer *)sender{
    
    NSInteger tag = sender.view.tag;
    NSDictionary *dict = self.ZMXZSData[tag];
    ZMZSXiangQingViewController *detail=[[ZMZSXiangQingViewController alloc]init];
    detail.amDict=dict;
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)zmxzscreateBtnView{
    UIWindow * window = [[UIApplication sharedApplication].delegate window];
    
    CGFloat bottom = 0.0;
    if (@available(iOS 11.0, *)) {
        bottom = window.safeAreaInsets.bottom;
    } else {
        // Fallback on earlier versions
    }
    
    
    UIButton *zmxzsleftBtn=[[UIButton alloc]initWithFrame:CGRectMake((kZMXZSWidth / 4 - 30), kZMXZSHeight - bottom - 60 - 20, 60, 60)];
    [zmxzsleftBtn setImage:[UIImage imageNamed:@"zmxzs_btn_left.png"] forState:UIControlStateNormal];
    [zmxzsleftBtn addTarget:self action:@selector(zmxzspmzmxzsleftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zmxzsleftBtn];
    
    UIButton *zmxzscenterBtn=[[UIButton alloc]initWithFrame:CGRectMake((kZMXZSWidth / 2 - 40), kZMXZSHeight - bottom - 80 - 20, 80, 80)];
    [zmxzscenterBtn setImage:[UIImage imageNamed:@"zmxzs_btn_add.png"] forState:UIControlStateNormal];
    [zmxzscenterBtn addTarget:self action:@selector(zmxzspmzmxzscenterBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zmxzscenterBtn];
    
    UIButton *zmxzsrightBtn=[[UIButton alloc]initWithFrame:CGRectMake(kZMXZSWidth / 2 +  (kZMXZSWidth / 4 - 30), kZMXZSHeight - bottom - 60 - 20, 60, 60)];
    [zmxzsrightBtn setImage:[UIImage imageNamed:@"zmxzs_btn_right.png"] forState:UIControlStateNormal];
    [zmxzsrightBtn addTarget:self action:@selector(pmzmxzsrightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:zmxzsrightBtn];
}
-(void)zmxzspmzmxzsleftBtnClick{
    _zmxzsSelectIndex = 1;
    [self zmxzsaccountpmShowBUad];
    
}
-(void)zmxzspmzmxzscenterBtnClick{
    _zmxzsSelectIndex = 2;
    [self zmxzsaccountpmShowBUad];
    
}
-(void)pmzmxzsrightBtnClick{
    ZMZSGuanyuViewController *zmxzsabout=[[ZMZSGuanyuViewController alloc]init];
    [self.navigationController pushViewController:zmxzsabout animated:YES];
}

-(void)zmxzsaccountpmShowBUad{
    
    if (self.zmxzsSelectIndex== 1) {
        ZMZSFenLeiViewController *zmxzsfenlei=[[ZMZSFenLeiViewController alloc]init];
        [self.navigationController pushViewController:zmxzsfenlei animated:YES];
    }
    if (self.zmxzsSelectIndex == 2) {
        ZMZSXuanZeLeiXingViewController *zmxzspmadd=[[ZMZSXuanZeLeiXingViewController alloc]init];
        [self.navigationController pushViewController:zmxzspmadd animated:YES];
    }
    
}
@end
